package nancal.mp

import nancal.mp.db.mysql.dbr
import nbcp.base.comm.JsonMap
import nbcp.base.enums.NameMappingTypeEnum
import nbcp.base.extend.minus
import nbcp.base.utils.FileUtil
import nbcp.base.utils.MyUtil
import nbcp.base.utils.ResourceUtil
import nbcp.myoql.code.generator.db.mysql.SqlDbEntityGenerator
import nbcp.myoql.db.db
import org.junit.jupiter.api.Test
import java.io.File
import java.time.LocalDateTime

class DevTool : TestBase() {

    @Test
    fun GenEntity_MysqlTable() {
        dbr.base.getEntities().forEach { table ->
            SqlDbEntityGenerator.entity2Sql(table.systemDefineEntityType, NameMappingTypeEnum.UNDER_LINE)
                .apply {
                    println(this)
                }
        }

        dbr.system.getEntities().forEach { table ->
            SqlDbEntityGenerator.entity2Sql(table.systemDefineEntityType, NameMappingTypeEnum.UNDER_LINE)
                .apply {
                    println(this)
                }
        }
    }


    /**
     * 生成数据库Markdown文档。
     * 需要把 MainApplication 中，取消 exclude DataSourceAutoConfiguration!
     * 在生成后，再利用工具，把 markdown 转为 pdf！
     */
    @Test
    fun entity_sql_markdown_group() {
        var style = """
<style>
${ResourceUtil.readResourceContentText("markdown.css")}
</style>
"""
        SqlDbEntityGenerator
            .db2Entity()
            .toMarkdown()
            .filter { it.name != "flyway_schema_history" }
            .forEach {
                println(it.name)

                var file = File(FileUtil.resolvePath(projectPath, "db-markdown", it.name + ".md"));
                file.parentFile.mkdirs();
                file.writeText(
                    it.code + style
                )
            }
    }


    @Test
    fun entity_sql_markdown_all_in_one() {
        var style = """
<style>
${ResourceUtil.readResourceContentText("markdown.css")}
</style>
"""
        var content = SqlDbEntityGenerator
            .db2Entity()
            .toMarkdown()
            .filter { it.name != "flyway_schema_history" }
            .map {
                it.code
            }
            .joinToString("\n") + style

        var file = File(FileUtil.resolvePath(projectPath, "db-markdown", "db.md"));
        file.parentFile.mkdirs();
        file.writeText(
            content
        )
    }

    @Test
    fun GenEntity_Sql_Entity() {
        SqlDbEntityGenerator.db2Entity()
            .toKotlinCode("a.b")
            .forEach {
                println()
                println("/** ${it.name}.kt **/")
                println(it.code)
                println()
            }
    }


    @Test
    fun TestInsert() {

        var startAt = LocalDateTime.now();

        for (i in 1..9999) {
            var map = JsonMap();
            map.put("id", "id_" + i)
            map.put("name", "name_" + i)
            map.put("remark", MyUtil.getRandomWithLength(30));
            map.put("a", MyUtil.getRandomWithLength(30));
            map.put("b", MyUtil.getRandomWithLength(30));
            map.put("c", MyUtil.getRandomWithLength(30));
            map.put("d", MyUtil.getRandomWithLength(30));
            db.sql.rawUpdate("insert into t_user(id,name,remark,a,b,c,d) values (:id,:name,:remark,:a,:b,:c,:d) ")
                .params(map)
                .exec();
        }


        for (i in 1..9999) {
            var map = JsonMap();
            map.put("id", "id_" + i)
            map.put("name", "name_" + i)
            map.put("remark", MyUtil.getRandomWithLength(30));
            map.put("a", MyUtil.getRandomWithLength(30));
            map.put("b", MyUtil.getRandomWithLength(30));
            map.put("c", MyUtil.getRandomWithLength(30));
            map.put("d", MyUtil.getRandomWithLength(30));
            db.sql.rawUpdate("insert into t_role(id,name,remark,a,b,c,d) values (:id,:name,:remark,:a,:b,:c,:d) ")
                .params(map)
                .exec();
        }

        for (i in 5..5999) {
            var map = JsonMap();
            map.put("id", "id_" + i)
            map.put("user_id", "id_" + i)
            map.put("role_id", "id_" + i);
            db.sql.rawUpdate("insert into t_user_role(id,user_id, role_id) values (:id,:user_id,:role_id) ")
                .params(map)
                .exec();
        }

        var endAt = LocalDateTime.now();

        println("总共耗时: ${endAt.minus(startAt).seconds}")

        //mariadb: 78
        //postgres: 78
    }

    @Test
    fun TestQueryEquals() {
        var startAt = LocalDateTime.now();

        for (i in 1..9999) {
            db.sql.rawQuery(
                """SELECT *
FROM t_user_role AS ur
JOIN t_user  AS u ON ( ur.user_id = u.id )
JOIN t_role AS r ON ( ur.role_id = r.id )
WHERE u.id = '${MyUtil.getRandomWithLength(4)}' """
            ).toMap();
        }
        var endAt = LocalDateTime.now();

        println("总共耗时: ${endAt.minus(startAt).seconds}")

        //postgres: 35
        //mariadb: 30
    }

    @Test
    fun TestQueryLike() {
        var startAt = LocalDateTime.now();

        for (i in 1..9999) {
            db.sql.rawQuery(
                """SELECT *
FROM t_user_role AS ur
JOIN t_user  AS u ON ( ur.user_id = u.id )
JOIN t_role AS r ON ( ur.role_id = r.id )
WHERE u.name LIKE '%${MyUtil.getRandomWithLength(3)}%' """
            ).toMap();
        }
        var endAt = LocalDateTime.now();

        println("总共耗时: ${endAt.minus(startAt).seconds}")

        //postgres: 45
        //mariadb: 117
    }
}